﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace rInvoice
{
    public partial class UsersAdministration : UserControl
    {
        ServerObject mServerObject = null;
        string messageText = string.Empty;

        public UsersAdministration()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }
// text

        private void CloseThisTab()
        {
            TabPage tabpage = (TabPage)this.Parent;
            TabControl tabControl = (TabControl)tabpage.Parent;
            tabControl.TabPages.Remove(tabpage);
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            CloseThisTab();
        }

        private void UsersAdministration_Load(object sender, EventArgs e)
        {
            FillGridView();
        }

        private void FillGridView()
        {
            DataTable gvTable = mServerObject.SelectUsers(false);
            dataGridView1.DataSource = gvTable;
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            
            UsersInputPanel AddForm = new UsersInputPanel();
            AddForm.CreateNew = true;

            DialogResult resultDialog = AddForm.ShowDialog();
            if (resultDialog == DialogResult.OK)
            {
                messageText = "Data was saved";
            }
            else
            {
                messageText = "Sorry, data was not saved";
            }
            MessageBox.Show(messageText);
            FillGridView();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int mUserID = 0;
                int mRole = 0;
                int mPasswordStatus = 0;
                int mRecordStatus = 0;

                string userID = dataGridView1.Rows[e.RowIndex].Cells["UserID"].Value.ToString();

                int.TryParse(userID, out mUserID);
                DataRow rowData = mServerObject.SelectUsersByID(mUserID);
                UsersInputPanel userInputPanel = new UsersInputPanel();

                userInputPanel.UserID = mUserID;
                userInputPanel.FirstName = rowData["FirstName"].ToString();
                userInputPanel.LastName = rowData["LastName"].ToString();
                userInputPanel.Loghin = rowData["Login"].ToString();
                userInputPanel.Password = rowData["Password"].ToString();
                //for confirm password
                int.TryParse(rowData["RoleType"].ToString(), out mRole);
                userInputPanel.Role = mRole;

                int.TryParse(rowData["PasswordStatus"].ToString(), out mPasswordStatus);
                userInputPanel.PasswordStatus = mPasswordStatus;

                int.TryParse(rowData["RecordStatus"].ToString(), out mRecordStatus);
                userInputPanel.RecordStatus = mRecordStatus;

                try
                {
                    DialogResult resultDialog = userInputPanel.ShowDialog();
                    if (resultDialog == DialogResult.OK)
                    {
                        messageText = "Data was saved";
                    }
                    else
                    {
                        messageText = "Sorry, data was not saved";
                    }
                    MessageBox.Show(messageText);
                }
                finally
                {

                }
                FillGridView();
            }
        }
    }
}
